﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Practices.Unity;

namespace Kkc.Common
{
    public class IocContainer: IIocContainer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="IocContainer"/> class.
        /// </summary>
        private IocContainer()
        {
        }

        private readonly IUnityContainer unityContainer = new UnityContainer();

        public object Resolve(Type type)
        {
            return unityContainer.Resolve(type);
        }

        public object Resolve(Type type, string name)
        {
            return unityContainer.Resolve(type, name);
        }

        public T Resolve<T>()
        {
            return unityContainer.Resolve<T>();
        }

        public T Resolve<T>(string name)
        {
            return unityContainer.Resolve<T>(name);
        }

        public IEnumerable<object> ResolveAll(Type type)
        {
            return unityContainer.ResolveAll(type);
        }

        public IEnumerable<T> ResolveAll<T>()
        {
            return unityContainer.ResolveAll<T>();
        }

        private static readonly IocContainer current = new IocContainer();

        public static IocContainer Current
        {
            [DebuggerStepThrough]
            get { return current; }
        }

        void IIocContainer.RegisterInstance<T>(T instance)
        {
            unityContainer.RegisterInstance<T>(instance);
        }

        void IIocContainer.RegisterInstance<T>(T instance, string name)
        {
            unityContainer.RegisterInstance<T>(name, instance);
        }

        void IIocContainer.RegisterType<TFrom, TTo>()
        {
            unityContainer.RegisterType<TFrom, TTo>();
        }

        void IIocContainer.RegisterType<TFrom, TTo>(string name)
        {
            unityContainer.RegisterType<TFrom, TTo>(name);
        }

        public void Teardown(object instance)
        {
            unityContainer.Teardown(instance);
        }


        public void RegisterType(Type from, Type to)
        {
            unityContainer.RegisterType(from, to);
        }

        public void RegisterType(Type from, Type to, string name)
        {
            unityContainer.RegisterType(from, to, name);
        }
    }
}
